package com.modudf.util
{
	import com.modudf.common.Global;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.utils.StringUtil;
	
	
	public class Connect
	{
		
		[Event(name="requestFail", type="REQUEST_FAILED")]
		
		/**
		 * @param JSON对象<br>
		 * {<br>
		 * url:"http://xxx.do", (默认为"") <br>
		 * params: URLParameter,  (默认为null)<br>
		 * callback: 回调Function, (默认为null)<br>
		 * failback: 失败回调Function, (默认为null)<br>
		 * isJson: 返回Json对象否则返回字符串,(默认为true)<br>
		 * }
		 * */
		public static function request(o:ConnectParameter):void
		{
			var url:String = o.url;
			var params:URLVariables = o.params
			var callback:Function = o.callback;
			var isJson:Boolean = o.isJson
			var failback:Function = o.failback;
			[Embed(source="assets/error.png")]
			var errSkin:Class;
			
			var alert:Alert;
			var err:Boolean = false;
			var nocache:Date = new Date();
			if (url.indexOf("?") == -1)
				url=url + "?_nocache=" + String(nocache.getTime());
			else
				url=url + "&_nocache=" + String(nocache.getTime());
			var req:URLRequest=new URLRequest(url);
			if (params != null)
				req.data = params;
			req.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, function(e:Event):void
			{
				var s:String = String(loader.data);
				if (StringUtil.isWhitespace(s))
				{
					alert = Alert.show("服务器异常！", "异常");
					alert.titleIcon = errSkin;
					err = true;
					return;
				}
				if (callback != null)
				{
					if (!isJson)
					{
						callback(s, e);
					}
					else
					{
						var r:Object = new Object()
						if (url.indexOf(".json") == -1)
							r = JSON.parse(String(loader.data));
						else
							r = JSON.parse(String(loader.data).replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "").replace(/ /g, ""));
						
						if (r != null && r["success"] == "true")
						{
							//请求成功
							callback(r, e);
						}
						else
						{
							//请求失败
							var errorMessage:String = r["errorMsg"];
							if (!err) {
								alert = Alert.show(errorMessage, "异常 ", 4, null, function(event:CloseEvent):void {
									if (errorMessage == "登录已失效") {
										navigateToURL(new URLRequest(Global.indexURL),"_self");
									}
								});
								alert.titleIcon = errSkin;
								err = true;
							}
							if (failback != null)
							{
								failback(r, e);
							}
						}
					}
				}
			});
			loader.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void
			{
				if (!err)
				{
					alert = Alert.show("网络通讯异常！", "异常");
					alert.titleIcon = errSkin;
					err = true;
				}
				if (failback != null)
				{
					failback(null, event);
				}
			});
			loader.load(req);
		}
	}
}